/**
 * @author ZhiWen Ren
 * @version 1.0
 * @description: LeetCode-35-搜索插入位置
 * @date 2025/5/16 11:29
 */
public class Solution35 {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            // left一定会和right相等的
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                // 找到了
                return mid;
            } else if (target < nums[mid]) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        // 没找到
        return left;
    }
}
